//
//  ButtonStyle.swift
//  EasyCoding
//
//  Created by Fanxx on 2019/6/3.
//  Copyright © 2019 Fanxx. All rights reserved.
//

import UIKit

extension UCSetting where TargetType: UIButton {
    ///文本
    public static func text(_ text:String?) -> UCSetting<TargetType> {
        return self.text(text, for: .normal)
    }
    ///文本
    public static func text(_ text:String?, for state: UIControl.State) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setTitle(text, for: state)
        })
    }
    ///富文本
    public static func text(_ text:NSAttributedString?) -> UCSetting<TargetType> {
        return self.text(text, for: .normal)
    }
    ///富文本
    public static func text(_ text:NSAttributedString?, for state: UIControl.State = .normal) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setAttributedTitle(text, for: state)
        })
    }
    ///图片
    public static func image(_ image:UIImage?) -> UCSetting<TargetType> {
        return self.image(image, for: .normal)
    }
    ///图片
    public static func image(_ image:UIImage?, for state: UIControl.State) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setImage(image, for: state)
        })
    }
    ///图片
    public static func image(_ named:String, for state: UIControl.State = .normal) -> UCSetting<TargetType> {
        return Self.image(UIImage(named: named), for: state)
    }
    ///图片
    public static func image(_ named:String, resizable insets: UIEdgeInsets, for state: UIControl.State = .normal) -> UCSetting<TargetType> {
        return Self.image(UIImage(named: named)?.resizableImage(withCapInsets: insets, resizingMode: .stretch), for: state)
    }
    ///字体
    public static func font(_ font:UIFont?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.titleLabel?.font = font
        })
    }
    ///系统字体
    public static var systemFont:UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: UIFont.systemFontSize))}
    ///系统字体
    public static func font(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: size))}
    ///系统字体
    public static func font(size: CGFloat, weight: UIFont.Weight) -> UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: size, weight: weight))}
    ///系统字体
    public static func boldFont(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.boldSystemFont(ofSize: size))}
    ///系统字体
    public static func italicFont(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.italicSystemFont(ofSize: size))}
    
    ///文本颜色
    public static func color(_ color:UIColor?) -> UCSetting<TargetType> {
        return self.color(color, for: .normal)
    }
    ///文本颜色
    public static func color(_ color:UIColor?, for state:UIControl.State...) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            if state.count > 0 {
                for s in state {
                    target.setTitleColor(color, for: s)
                }
            }else{
                target.setTitleColor(color, for: .normal)
            }
        })
    }
    ///背景图片
    public static func bg(_ image:UIImage?, for state:UIControl.State = .normal) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setBackgroundImage(image, for: state)
        })
    }
    ///背景图片
    public static func bg(_ imageNamed:String, for state:UIControl.State = .normal) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setBackgroundImage(UIImage(named: imageNamed), for: state)
        })
    }
    ///背景颜色
    public static func bg(_ color:UIColor?) -> UCSetting<TargetType> {
        return self.bg(color, for: .normal)
    }
    ///背景颜色
    public static func bg(_ color:UIColor?, for state:UIControl.State) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            if state == .normal {
                target.backgroundColor = color
            }else{
                if let c = color {
                    let rect = CGRect(origin: .zero, size: CGSize(width: 1, height: 1))
                    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
                    let context = UIGraphicsGetCurrentContext()!
                    context.setFillColor(c.cgColor)
                    context.fill(rect)
                    let image = UIGraphicsGetImageFromCurrentImageContext()
                    UIGraphicsEndImageContext()
                    target.setBackgroundImage(image, for: state)
                }else{
                    target.setBackgroundImage(nil, for: state)
                }
            }
        })
    }
    ///最大显示行数
    public static func lines(_ lines:Int = 0) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.titleLabel?.numberOfLines = lines
        })
    }
    ///边距
    public static func edge(insets:UIEdgeInsets) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.contentEdgeInsets = insets
        })
    }
    ///边距
    public static func title(insets:UIEdgeInsets) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.titleEdgeInsets = insets
        })
    }
    ///边距
    public static func image(insets:UIEdgeInsets) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.imageEdgeInsets = insets
        })
    }
    ///字体最小比例
    public static func min(scale:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.titleLabel?.adjustsFontSizeToFitWidth = false
            target.titleLabel?.minimumScaleFactor = scale
        })
    }
    ///字体最小尺寸
    public static func min(size:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.titleLabel?.adjustsFontSizeToFitWidth = true
            target.titleLabel?.minimumScaleFactor = size
        })
    }
    ///是否自动适配图片点击(变灰)和不可用效果
    public static func adjusts(highlighted: Bool = true, disabled: Bool = false) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.adjustsImageWhenHighlighted = highlighted
            target.adjustsImageWhenDisabled = disabled
        })
    }
}
